/*
*
==============================================================================
*  Name        : 
*  Part of     : Omen Game Engine
*  Description : 
*  Version     : 1.0
*
*  Copyright (c) 2013 Lauri Kortevaara.
* 
==============================================================================
*/

#define OMEN_SOURCE 1

/// Omen Headers
#include "OmenStorageServer.h"

/// STL Headers
#include <fstream>

using namespace Omen;

StorageServer::StorageServer()
{
}

StorageServer::~StorageServer() 
{
    // Delete archive file readers
    for( StorageServer::ArchiveFileReaders::reverse_iterator it = m_archiveReaders.rbegin();
         it != m_archiveReaders.rend();
        ++it ) 
    {
        delete *it;
    }

    // Delete model file readers
    for( StorageServer::ArchiveFileReaders::reverse_iterator it = m_archiveReaders.rbegin();
         it != m_archiveReaders.rend();
        ++it ) 
    {
        delete *it;
    }
}

void StorageServer::addArchiveReader( Omen::IArchiveReader* pArchiveReader)
{
    m_archiveReaders.push_back(pArchiveReader);
}

/// <summry>Opens an archive by searching for a matching archive reader</summary>
/// <param name="filename">File a reader will be searched for</param>
IArchive* StorageServer::openArchive(const std::wstring &filename) 
{
    for( auto iter = m_archiveReaders.begin(); iter != m_archiveReaders.end(); ++iter ) 
    {
        if((*iter)->canOpenArchive(filename))
            return (*iter)->openArchive(filename);
    }

    throw std::runtime_error("Invalid or unsupported archive type");
}

/// <summary>Allows plugins to add new model file readers</summary>
void StorageServer::addModelFileReader( Omen::IModelFileReader* pModelFileReader)
{
    m_modelFileReaders.push_back(pModelFileReader);
}

/// <summry>Returns a modelFileReader by searching for a matching modelFileRedaer</summary>
IModelFileReader* StorageServer::getModelFileReader(const std::wstring& filename)
{
    if( !std::wifstream(filename) )
    {
        std::string strFile(filename.begin(), filename.end());
        std::string error = "Omen::StorageServer file not found Error! Cannot find file " + strFile; 
        throw std::runtime_error(error);
    }

    for(auto iter = m_modelFileReaders.begin(); iter != m_modelFileReaders.end(); ++iter ) 
    {
        if((*iter)->canRead(filename))
            return *iter;
    }

    throw std::runtime_error("Invalid or unsupported Model type");
}

/// <summary>Allows plugins to add new image file readers</summary>
void StorageServer::addImageFileReader( Omen::IImageFileReader* pImageFileReader)
{
    m_imageFileReaders.push_back(pImageFileReader);
}

/// <summry>Returns a IImageFileReader by searching for a matching modelFileRedaer</summary>
IImageFileReader* StorageServer::getImageFileReader(const std::wstring& filename)
{
    if( !std::wifstream(filename) )
    {
        std::string strFile(filename.begin(), filename.end());
        std::string error = "Omen::StorageServer file not found Error! Cannot find file " + strFile; 
        throw std::runtime_error(error);
    }

    for( auto iter = m_imageFileReaders.begin(); iter != m_imageFileReaders.end(); ++iter ) 
    {
        if((*iter)->canRead(filename))
            return *iter;
    }

    throw std::runtime_error("Invalid or unsupported image type");
}
